package org.activiti.process;

import java.util.HashMap;
import java.util.Map;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.activiti.engine.runtime.ProcessInstance;

import org.apache.log4j.Logger;

public class RozdeleniNaSubprocesy implements JavaDelegate {

    private static Logger logger = Logger.getLogger("RozdeleniNaSubprocesy");

    @Override
    public void execute(DelegateExecution arg0) throws Exception {
        int count = 20;
        logger.info("For task " + arg0.getProcessInstanceId());
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        final String id = arg0.getProcessInstanceId();
        final RuntimeService runtimeService = processEngine.getRuntimeService();
        String[] subProcesses = new String[count];
        for (int i = 0; i < count; i++) {
            Map<String, Object> variables = new HashMap<String, Object>();
            variables.put("processId", id);
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("testWFIE", variables);
            subProcesses[i] = processInstance.getProcessInstanceId();
            logger.info(processInstance.getProcessInstanceId());
        }
        arg0.setVariable("ids", subProcesses);
    }

}
